package com.hb.software.controller.profit;

import com.hb.software.controller.BaseController;
import com.hb.software.domain.Result;
import com.hb.software.entity.profit.Profit;
import com.hb.software.entity.profit.ProfitMessage;
import com.hb.software.profit.ProfitService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("profit")
public class ProfitController extends BaseController<ProfitMessage,Long,ProfitService> {

    private static final Logger logger = LogManager.getLogger(ProfitController.class.getName());

    private ProfitService profitService;

    @Autowired
    public void setProfitService(ProfitService profitService) {
        this.profitService = profitService;
        this.manager = this.profitService;
    }
    @PostMapping("/findByName")
    public Result findByName(@RequestBody String name) throws UnsupportedEncodingException {
        //处理中文乱码问题
        name = URLDecoder.decode(name,"UTF-8");
        name = name.substring(0,name.length()-1);
        List list=profitService.findByName(name);
        return Result.success(list);
    }
    @PostMapping("/findLastId")
    public Result findLastId()   {
        long id = profitService.findLastId();
        return Result.success(id);
    }

    @PostMapping("/exportExcel")
    public Result exportExcel(@RequestBody String path)throws UnsupportedEncodingException {
        //处理中文乱码问题
        path = URLDecoder.decode(path,"UTF-8");
        path = path.substring(0,path.length()-1);
        List list = profitService.findAll();

        String str = profitService.createExcel(list,path);
        if(str.equals("success"))
            return Result.success();
        else return Result.error("路径错误");
    }


    @PostMapping("/findByDate")
    public Result findByDate(@RequestBody String date){
        List list = new ArrayList();
        try{
            list=profitService.findByDate(date.substring(0,date.length()-1));
        }catch (Exception e){

        }
        return Result.success(list);
    }
}



